<?php
// error_reporting(E_ERROR);
// dirname(__FILE__)
// chdir(dirname(__FILE__));
// require_once ('../log.php');
// require_once ("api_tool.php");
// 演示网址 http://ipad.yykjgame.com/swagger/ui/index#

// $address = "localhost:62629";
$address = "http://ipad.yykjgame.com";
define("IPAD_API_URL", 		"{$address}/");
define("IPAD_LOGIN_URL",	"{$address}/api/Login/");
define("IPAD_MESSAGE_URL", 	"{$address}/api/Message/");
define("IPAD_PROFILE_URL", 	"{$address}/api/Profile/");
define("IPAD_FRIEND_URL", 	"{$address}/api/friend/");

// 登录
function ipad_login() {
	$response = "{}";
	$url = IPAD_LOGIN_URL.'GetQrCode';
	return json_decode(ipadSendSGHttp($url, $response, "post"), true);
}

// 获取登录状态
function ipad_get_login_state($uuid) {
	$response = '';
	$url = IPAD_LOGIN_URL.'CheckLogin/'.$uuid;
	return json_decode(ipadSendSGHttp($url, $response, "post"), true);
}

// 退出微信
function ipad_exit($wx_id) {
	// LOGINFO($wx_id, '=== $wx_id');
	$response = '';
	$url = IPAD_LOGIN_URL.'Logout/'.$wx_id;
	return json_decode(ipadSendSGHttp($url, $response, "post"), true);
}

// 发送文字
function ipad_send_text_msg($rob_wxid, $to_wxid, $msg){
	// 封装返回数据结构
	$response = json_encode(array(
		"toWxIds" 	=> [$to_wxid],
		"content" 	=> $msg,
		"wxId"		=> $rob_wxid
	));
	// 调用Api组件
	$url = IPAD_MESSAGE_URL.'SendTxtMessage';
	return json_decode(ipadSendSGHttp($url, $response, 'post'), true);
}

// 发送群消息并艾特某人
function ipad_send_group_at_msg($rob_wxid, $to_wxid, $at_wxid, $at_name, $msg){
	// 封装返回数据结构
	$response = json_encode(array(
		"toWxIds" 	=> [$to_wxid],
		"atWxIds" 	=> [$at_wxid],
		"content" 	=> $msg,
		"wxId"		=> $rob_wxid,
	));

	$response = array('data' => json_encode($data));

	// 调用Api组件
	$url = IPAD_MESSAGE_URL.'SendTxtMessage';
	return ipadSendSGHttp($url, $response, 'post');
}

// 发送图片
function ipad_send_image_msg($rob_wxid, $to_wxid, $path) {
	$img_info = getimagesize($path); // 取得图片的大小，类型等
	switch ($img_info[2]) {           //判读图片类型
        case 1: $img_type = "gif";
            break;
        case 2: $img_type = "jpg";
            break;
        case 3: $img_type = "png";
            break;
	}
	
	$image_data = fread(fopen($path, 'r'), filesize($path));
	$img_base64 = "data:image/{$img_type};base64,".chunk_split(base64_encode($image_data));
	// 封装返回数据结构
	$response = json_encode(array(
		"toWxIds" 	=> [$to_wxid],
		"base64" 	=> $img_base64,
		"wxId"		=> $rob_wxid,
	));

	// 调用Api组件
	$url = IPAD_MESSAGE_URL.'SendImageMessage';
	return ipadSendSGHttp($url, $response, 'post');
}

// 发送视频消息
function ipad_send_video_msg($rob_wxid, $to_wxid, $path){
	$response = json_encode(array(
		"toWxIds" 		=> [$to_wxid],
		"playLength"	=> 0,
		"base64"		=> "string",
		"imageBase64"	=> "string",
		"wxId"			=> $rob_wxid,
	));

	// 调用Api组件
	$url = IPAD_MESSAGE_URL.'SendVideoMessage';
	return ipadSendSGHttp($url, $response, 'post');
}

// 发送文件消息
function ipad_send_file_msg($rob_wxid, $to_wxid, $path) {
	// 封装返回数据结构
	$response = json_encode(array(
		"toWxIds" 	=> [$to_wxid],
		"attachId"	=> "string",
		"title"		=> "string",
		"length"	=> 0,
		"fileExt"	=> "string",
		"wxId"		=> $rob_wxid,
	));

	// 调用Api组件
	$url = IPAD_MESSAGE_URL.'SendMediaMessage';
	return ipadSendSGHttp($url, $response,'post');
}

// 发送动态表情
function ipad_send_emoji_msg($rob_wxid, $to_wxid, $path) {
	// 封装返回数据结构
	$response = json_encode(array(
		"toWxIds" 	=> [$to_wxid],
		"type" 		=> 0,
		"content" 	=> "string",
		"md5" 		=> "string",
		"wxId"		=> $rob_wxid,
	));

	// 调用Api组件
	$url = IPAD_MESSAGE_URL.'SendEmojiMessage';
	return ipadSendSGHttp($url, $response, 'post');
}

// 获取微信的心跳
function ipad_get_heart_beat($wx_id) {
	// 发送心跳
	$response = '';
	$url = IPAD_PROFILE_URL.'HeartBeat/'.$wx_id;
	return json_decode(ipadSendSGHttp($url, $response, "post"), true);
}

// 同步微信消息
function ipad_get_sync_message($wx_id) {
	$response = '';
	$url = IPAD_MESSAGE_URL.'SyncMessage/'.$wx_id;
	return json_decode(ipadSendSGHttp($url, $response, "post"), true);
}

// 查询微信群信息
function ipad_get_contract_detail($wxid, $to_wxid) {
	// 封装返回数据结构
	$response = json_encode(array(
		"searchWxIds" 	=> [$to_wxid],
		"wxId" 			=> $wxid,
	));

	// 调用Api组件
	$url = IPAD_FRIEND_URL.'GetContractDetail';
	return json_decode(ipadSendSGHttp($url, $response, 'post'), true);
}

// 微信统一回复接口
function ipad_send_msg($task_info, $_msg) {
	$rob_wxid = $task_info['bot_wxid'];
	$bot_name = $task_info['bot_name'];
	$from_wxid = $task_info['from_wxid'];
	$from_name = $task_info['from_name'];
	$final_from_wxid = $task_info['final_from_wxid'];
	$final_from_name = $task_info['final_from_name'];
	$msg_type = $_msg['msg_type'];
	$msg = $_msg['msg'];
	// LOGINFO($rob_wxid, "=== rob_wxid");
	// LOGINFO($from_wxid, "=== from_wxid");
	if ($msg_type == 1) {
		LOGINFO("回复文字");
		return ipad_send_text_msg($rob_wxid, $from_wxid, $msg);
	}
	else if ($msg_type == 3) {
		LOGINFO("回复图片");
		return ipad_send_image_msg($rob_wxid, $from_wxid, $msg);
	}
	else if ($msg_type == 43) {
		LOGINFO("回复视频");
		return ipad_send_video_msg($rob_wxid, $from_wxid, $msg);
	}
	else if ($msg_type == 2004) {
		LOGINFO("回复文件");
		return ipad_send_file_msg($rob_wxid, $from_wxid, $msg);
	}
	else if ($msg_type == 47) {
		LOGINFO("回复表情");
		return ipad_send_emoji_msg($rob_wxid, $from_wxid, $msg);
	}
	else if ($msg_type == 1024) {
		LOGINFO("@所有人");
		return ipad_send_group_at_msg($rob_wxid, $from_wxid, $final_from_wxid, "所有人", "");
	}
}

function ipadSendSGHttp($url, $params, $method = 'get', $timeout = 3) {
	if (null == $url) return null;

	$curl = curl_init();

	if ('get' == $method) {//以GET方式发送请求
		curl_setopt($curl, CURLOPT_URL, $url);
	} else { // 以POST方式发送请求
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_POST, 1);// post提交方式
		curl_setopt($curl, CURLOPT_POSTFIELDS, $params);//设置传送的参数
	}

	$header = array('Content-Type: application/json'); //设置一个你的浏览器agent的header
	curl_setopt($curl, CURLOPT_HEADER, false);//设置header
	curl_setopt($curl, CURLOPT_HTTPHEADER, $header);//设置header
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//要求结果为字符串且输出到屏幕上
	curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);//设置等待时间

	$res = curl_exec($curl);//运行curl
	$err = curl_error($curl);

	if (false === $res || !empty($err)) {
		$Errno = curl_errno($curl);
		$Info = curl_getinfo($curl);
		curl_close($curl);

		//print_r($Info);

		return $err. ' result: ' . $res . 'error_msg: '.$Errno;
	}
	curl_close($curl);//关闭curl
	return $res;
}

?>